package com.ruoyi.bizsys.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.bizsys.domain.ProductDeferredInfo;
import com.ruoyi.bizsys.domain.ProductInfoVo;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.service.IProductInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品信息Controller
 *
 * @author african-loan
 * @date 2021-12-30
 */
@RestController
@RequestMapping("/business/ProductInfo")
public class ProductInfoController extends BaseController {
    @Autowired
    private IProductInfoService productInfoService;

    @Log(title = "金融包产品", businessType = BusinessType.GET)
    @GetMapping("/getFinancial")
    public List<ProductDeferredInfo> getFinancial() {
        return productInfoService.getFinancial();
    }

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo) {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductInfo productInfo) {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "ProductInfo");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long productId) {
        return AjaxResult.success(productInfoService.selectProductInfoById(productId));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInfoVo productInfovo) {
        if (!StringUtils.isEmpty(productInfovo.getEndDate())&&!StringUtils.isEmpty(productInfovo.getStartDate())) {
            if (productInfovo.getEndDate().before(productInfovo.getStartDate())) {
                throw new BusinessException(RspEnmu.TIME_ERR);
            }
        }
        return toAjax(productInfoService.saveProductInfo(productInfovo));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfoVo productInfoVo) {
        return toAjax(productInfoService.updateProductInfo(productInfoVo));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }

}
